package com.sxt.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sxt.common.BussiException;
import com.sxt.common.CodeMsg;
import com.sxt.common.Constant;
import com.sxt.common.Result;
import com.sxt.domain.BusCar;
import com.sxt.domain.BusCustomer;
import com.sxt.domain.BusRent;
import com.sxt.mapper.BusCarDao;
import com.sxt.mapper.BusCustomerDao;
import com.sxt.mapper.BusRentDao;
import com.sxt.query.BusCustomerQuery;
import com.sxt.query.BusRentQuery;
import com.sxt.service.BusRentService;
import com.sxt.shiro.ActiveUser;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Description: 车辆出租记录业务接口实现
 */
@Service
public class BusRentServiceImpl implements BusRentService {
    @Autowired
    private BusCustomerDao busCustomerDao;

    @Autowired
    private BusCarDao busCarDao;

    @Autowired
    private BusRentDao busRentDao;

    /**
     * 新增车辆出租记录
     *
     * @param busRent
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result add(BusRent busRent) {
        // 业务数据校验
        // 根据身份证号查询客户 校验客户是否存在
        BusCustomerQuery query = new BusCustomerQuery();
        query.setIdCard(busRent.getIdCard());
        BusCustomer busCustomer = busCustomerDao.selectCustomerByKeywords(query);
        if (busCustomer == null) {
            return new Result(CodeMsg.RENT_CUSTOMER_ID_CARD_ERROR);
        }

        // 校验车辆的信息
        // 根据车牌号查询车辆信息
        BusCar busCar = busCarDao.selectOneByNum(busRent.getNum());
        // 获取出租状态 判断出租状态是否是 未出租
        if (busCar.getIsRent().equals(Constant.CAR_RENT_ED)) {
            return new Result(CodeMsg.RENT_CAR_RENTED_ERROR);
        }
        // 乐观锁处理并发问题
        // 修改车辆状态
        Integer rows = busCarDao.updateRentState(busCar.getId(), Constant.CAR_RENT_ED, busCar.getVersion());
        if (rows != 1) {
            throw new BussiException(CodeMsg.RENT_FAILED_ERROR.code, CodeMsg.RENT_FAILED_ERROR.msg);
        }
        String rentTime = busRent.getRentTime();
        String[] split = rentTime.split("~");
        busRent.setBeginTime(split[0].trim());
        busRent.setEndTime(split[1].trim());
        // 客户名称
        busRent.setName(busCustomer.getName());
        // 设置业务员ID
        Subject subject = SecurityUtils.getSubject();
        ActiveUser activeUser = (ActiveUser) subject.getPrincipal();
        busRent.setUserId(activeUser.getSysUser().getId());
        // 新增出租记录
        busRentDao.insert(busRent);
        return new Result();
    }

    /**
     * 出租记录的分页查询
     *
     * @param query
     * @return
     */
    @Override
    public Result queryPage(BusRentQuery query) {
        Page<BusRent> busRentVOPage = PageHelper.startPage(query.getPage(), query.getLimit());
        // 如果开始时间不为空
        if (StrUtil.isNotEmpty(query.getBeginTime())) {
            String[] split = query.getBeginTime().split("~");
            query.setMinBeginTime(split[0].trim());
            query.setMaxBeginTime(split[1].trim());
        }
        busRentDao.selectList(query);
        return new Result(busRentVOPage.toPageInfo());
    }
}
